package com.caregiver.watch.business.care.model.vo;

import com.caregiver.watch.common.utils.time.DateUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static com.caregiver.watch.common.constant.NumberConstants.BIG_DECIMAL_TEN_THOUSAND;
import static com.caregiver.watch.common.constant.NumberConstants.ONE;
import static com.caregiver.watch.common.constant.NumberConstants.TWO;

/**
 * @description: 体温详情数据
 * @program: caregiver-watch
 * @author: 疆戟
 * @create: 2025-10-08 12:26
 */
@Data
@Schema(description = "体温详情数据")
public class TemperatureDetailDataVo {

    // 平均体温
    @Schema(description = "平均体温")
    private BigDecimal avgTemperature;

    // 最高温度
    @Schema(description = "最高温度")
    private BigDecimal maxTemperature;

    // 最低温度
    @Schema(description = "最低温度")
    private BigDecimal minTemperature;

    // 当天体温数据列表
    @Schema(description = "当天体温数据列表")
    private List<TemperatureData> temperatureDataList;

    public static TemperatureDetailDataVo build(BigDecimal avgTemperature, BigDecimal maxTemperature, BigDecimal minTemperature, List<TemperatureData> temperatureDataList) {
        TemperatureDetailDataVo temperatureDetailDataVo = new TemperatureDetailDataVo();
        temperatureDetailDataVo.avgTemperature = Optional.ofNullable(avgTemperature).map(temp -> temp.divide(BIG_DECIMAL_TEN_THOUSAND, ONE)).orElse(BigDecimal.ZERO);
        temperatureDetailDataVo.maxTemperature = Optional.ofNullable(maxTemperature).map(temp -> temp.divide(BIG_DECIMAL_TEN_THOUSAND, ONE)).orElse(BigDecimal.ZERO);
        temperatureDetailDataVo.minTemperature = Optional.ofNullable(minTemperature).map(temp -> temp.divide(BIG_DECIMAL_TEN_THOUSAND, ONE)).orElse(BigDecimal.ZERO);
        temperatureDetailDataVo.temperatureDataList = temperatureDataList;
        return temperatureDetailDataVo;
    }

    @Data
    @Schema(description = "体温数据")
    public static class TemperatureData {

        @Schema(description = "测量时间(分钟)")
        private Integer time;

        @Schema(description = "体温")
        private BigDecimal temperature;

        public static TemperatureData build(LocalDateTime time, BigDecimal temperature) {
            TemperatureData temperatureData = new TemperatureData();
            temperatureData.time = DateUtils.getMinute(time);
            temperatureData.temperature = Optional.ofNullable(temperature).map(temp -> temp.divide(BIG_DECIMAL_TEN_THOUSAND, ONE)).orElse(BigDecimal.ZERO);
            return temperatureData;
        }
    }
}
